﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace GameName1
{
    class AngryAlienController : GameObject
    {
        private bool[] _conditions = new bool[4];
        private static AngryAlienState[] Cycle =
        {
            new AngryAlienStateMovingRight(),
            new AngryAlienStateMovingDown(),
            new AngryAlienStateMovingLeft(),
            new AngryAlienStateMovingDown()
        };

        private static int _cycleIndex;
              
        private double _lastMoveTime;
        public static double MoveDelay;

        public AngryAlienController()
        {
            AngryAlien.AngryAlienState = new AngryAlienStateMovingRight();
        }

        public override void Update(GameTime gameTime)
        {
            var currentTime = gameTime.TotalGameTime.TotalMilliseconds;
            if (currentTime > _lastMoveTime + MoveDelay)
            {
                _lastMoveTime = currentTime;
                var aliens = GameObjectsManager.GetInstance().GetAllObjectsOfType<AngryAlien>();
                var explosions = GameObjectsManager.GetInstance().GetAllObjectsOfType<AngryAlienExplosion>();


                foreach (IMovable angryAlien in aliens)
                {
                    angryAlien.Move();
                }

                foreach (var angryAlienExplosion in explosions)
                {
                    angryAlienExplosion.Move();
                }

                foreach (var angryAlien in aliens)
                {
                    if(angryAlien.Position.Y>ScreenManager.GetInstance().Resolution.Y-80)
                        ScreenManager.GetInstance().CurrentScreen = new GameOverScene(GameplayScene.Points);

                    _conditions[0] = angryAlien.Position.X > ScreenManager.GetInstance().Resolution.X - 50;
                    _conditions[1] = true;
                    _conditions[2] = angryAlien.Position.X < 50;
                    _conditions[3] = true;

                    if (_conditions[_cycleIndex % 4])
                    {
                        _cycleIndex++;
                        break;
                    }
                }
                AngryAlien.AngryAlienState = AngryAlien.AngryAlienState = Cycle[_cycleIndex%4];
            }
        }
        
    }
}
